package com.aaa.controller.goods;

import com.aaa.entity.Goods;
import com.aaa.service.GoodsService;
import com.aaa.service.impl.GoodsServiceImpl;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/UpdateGoodsServlet")
public class UpdateGoodsServlet extends HttpServlet {
    GoodsService goodsService = null;
    @Override
    public void init() throws ServletException {
        goodsService = new GoodsServiceImpl();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String goodsId = req.getParameter("goodsId");
        String goodsName = req.getParameter("goodsName");
        String code = req.getParameter("code");
        String unitId = req.getParameter("unitId");
        String type = req.getParameter("type");
        String price = req.getParameter("price");
        String categoryId = req.getParameter("categoryId");

        Goods goods = new Goods();
        if(goodsId!=null){
            goods.setGoodsId(goodsId);
            goods.setName(goodsName);
            goods.setCode(code);
            goods.setUnitId(Integer.valueOf(unitId));
            goods.setType(Integer.valueOf(type));
            goods.setPrice(Double.valueOf(price));
            goods.setCategoryId(Integer.valueOf(categoryId));

            int i = goodsService.updateGoods(goods);
            resp.getWriter().print(new Gson().toJson(goods));
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
